//
//  AddCoachViewController.m
//  YunDongAdmin
//
//  Created by lqq on 2018/9/13.
//  Copyright © 2018年 cloudfighting.com. All rights reserved.
//


typedef NS_ENUM(NSInteger,AddCoachVCShowStyle) {
    AddCoachVCShowStyle_Normal,//刚开始进来的状态
    AddCoachVCShowStyle_FindCoach,//搜索到教练状态
    AddCoachVCShowStyle_NoFindCoach,//未搜索到教练状态
};

#import "AddCoachViewController.h"
#import "I_Coach.h"
#import "ShareTool.h"

@interface AddCoachViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *navBackViewHeightConstraint;
@property (weak, nonatomic) IBOutlet UIView *searchBackView;
@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *helpViewHeightConstraint;

@property (weak, nonatomic) IBOutlet UILabel *helpDescLabel;

@property (weak, nonatomic) IBOutlet UIView *helpView;
@property (weak, nonatomic) IBOutlet UITableView *tableView;


@property (nonatomic, assign) AddCoachVCShowStyle showStyle;
@property (nonatomic, strong) M_Coach *coach;

@end

@implementation AddCoachViewController
#pragma mark Class Method
+ (instancetype)controller
{
    AddCoachViewController *vc = [UIStoryboard controllerWithStoryBoardIdentify:NSStringFromClass([AddCoachViewController class]) inStoryBoard:@"Group"];
    return vc;
}


- (void)setShowStyle:(AddCoachVCShowStyle)showStyle
{
    _showStyle = showStyle;
    switch (showStyle) {
        case AddCoachVCShowStyle_Normal:
        {
            _helpViewHeightConstraint.constant = 70;
            UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kDeviceWidth, 1)];
            self.tableView.tableHeaderView = headerView;
        }
            break;
        case AddCoachVCShowStyle_FindCoach:
        {
            _helpViewHeightConstraint.constant = 0;
            UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kDeviceWidth, 1)];
            self.tableView.tableHeaderView = headerView;

        }
            break;
        case AddCoachVCShowStyle_NoFindCoach:
        {
            _helpViewHeightConstraint.constant = 0;
            UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kDeviceWidth, 74)];
            headerView.backgroundColor = [UIColor whiteColor];
            UILabel *nofindLabel = [[UILabel alloc] init];
            nofindLabel.textColor = HEXColor(@"#4A4A4A ");
            nofindLabel.font = RegularFont(14);
            nofindLabel.numberOfLines = 0;
            NSString *coachCode = _textField.text;
            NSAttributedString *coachCodeAttStr = [[NSAttributedString alloc] initWithString:coachCode attributes:@{NSForegroundColorAttributeName:HEXColor(@"#3AA7FF")}];
            NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"没有找到ID：的教练\n你可以通过微信邀请加入：%@",RI.currentGym.gymName]];
            [str insertAttributedString:coachCodeAttStr atIndex:7];
            nofindLabel.attributedText = str;
            [headerView addSubview:nofindLabel];
            [nofindLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(headerView).offset(20);
                make.right.equalTo(headerView).offset(-20);
                make.top.equalTo(headerView).offset(15);
                make.bottom.equalTo(headerView).offset(-15);

            }];
            CGFloat headerViewHeight = [headerView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
            headerView.height = headerViewHeight;
            self.tableView.tableHeaderView = headerView;
        }
            break;
            
        default:
            break;
    }
    [self.tableView reloadData];
}
#pragma mark Life Circle
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self configUI];
    self.showStyle = AddCoachVCShowStyle_Normal;
//    _textField.text = @"10046149";
}

#pragma mark UI
- (void)configUI
{
    _navBackViewHeightConstraint.constant = [Toolkit getSystemNavHeight];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.insetTop = 10;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    if ([_tableView respondsToSelector:@selector(setContentInsetAdjustmentBehavior:)]) {
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:@"添加的教练，适用于登录，负责所在门店的健身会员管理、到店记录和排课安排等使用。"];
    [att insertAttributedString:[[NSAttributedString alloc] initWithString:@"【云动助教】APP（3.2及以上版本）" attributes:@{NSForegroundColorAttributeName:HEXColor(@"3AA7FF"),NSFontAttributeName:MediumFont(14)}] atIndex:11];
    _helpDescLabel.attributedText = att;

}

#pragma mark NET
- (void)requstCoachByCoachCode
{
    [AV showLoading:@"请稍后"];
    WeakSelf(self);
    [I_Coach requestCoachAccountByCoachCode:_textField.text success:^(M_Coach *coach) {
        [AV hideen];
        if (coach) {
            weakself.coach = coach;
            weakself.showStyle = AddCoachVCShowStyle_FindCoach;
        } else {
            weakself.showStyle = AddCoachVCShowStyle_NoFindCoach;
        }
    } failure:^(NSError *error) {
        [AV showError:error];
    }];
}

#pragma mark Action
- (IBAction)goBack:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)searchCoach:(id)sender {
    [self requstCoachByCoachCode];
}

#pragma mark UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (_showStyle) {
        case AddCoachVCShowStyle_FindCoach:
        {
            AddCoachCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([AddCoachCell class])];
            cell.coach = _coach;
            return cell;
        }
            break;
            
        default:
        {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WeChatInviteCell"];
            return cell;
        }
            break;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (![cell isKindOfClass:[AddCoachCell class]]) {
        //H5邀请
        NSString *router = [NSString stringWithFormat:@"/manage/dist/index.html#/invitation/%@/%@",RI.currentGym.gymId,RI.account.brandName];
        NSString *webUrl = Url_ForInviteCoachJoinGym(router);
        NSString *shareTitle = [NSString stringWithFormat:@"%@邀请你加入%@",RI.account.brandName,RI.currentGym.gymName];
        [ShareTool shareWebUrlToPlatorm:SharePlatformType_WechatSession webUrl:webUrl title:shareTitle desc:@"加入门店，使用云动助教App管理会员健身档案更加方便，赶紧用起来" thumImage:RI.currentGym.gymLogo currentController:self];
        
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (_showStyle == AddCoachVCShowStyle_NoFindCoach) {
        return 10;
    }
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.001;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}
@end


@interface AddCoachCell ()
@property (weak, nonatomic) IBOutlet UIImageView *avaterImageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *mobileLabel;
@property (weak, nonatomic) IBOutlet UIButton *inviteBtn;
@end

@implementation AddCoachCell
-(void)awakeFromNib
{
    [super awakeFromNib];
    _inviteBtn.layer.cornerRadius = 2;
    _inviteBtn.layer.masksToBounds = YES;
    _avaterImageView.layer.cornerRadius = 2;
    _avaterImageView.layer.masksToBounds = YES;
}

- (void)setCoach:(M_Coach *)coach
{
    _coach = coach;
    [_avaterImageView setAvaterImageWithUrl:coach.avatarUrl];
    _nameLabel.text = coach.coachName;
    _mobileLabel.text = coach.coachMobile;
}
- (IBAction)inviteCoach:(id)sender {
    [AV showLoading:@"请稍后"];
    [I_Coach inviteCoachWithCoachId:_coach.coachId success:^{
        [AV showSuccess:@"发送邀请成功"];
    } failure:^(NSError *error) {
        [AV showError:error];
    }];
}

@end
